//
//  ServiceTool.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/16.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "ServiceTool.h"
#import "CYXBaseRequest.h"

@interface ServiceTool()
/// <#注释#>
@property (nonatomic, copy) NSString *firstProxyAddr;
@end
@implementation ServiceTool


- (void)getAllInterfaceSuccess:(void (^)(id))success failure:(void (^)(id))failure{
    

    [CYXBaseRequest postWithUrl:API_getProxyListAll param:nil resultClass:nil success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            self.proxyList = [ProxyModel mj_objectArrayWithKeyValuesArray:res[@"list"]];
        }
        success(res);
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
        failure(error);
    }];
}



- (void)pickTheBestInterface{
    [self getAllInterfaceSuccess:^(id res) {
        [self getProxySpeed];
    } failure:^(id err) {
        ;
    }];
}

- (void)getProxySpeed{
    self.firstProxyAddr = @"";
    for (ProxyModel *item in self.proxyList) {
        NSString *ip = [NSString stringWithFormat:@"http://%@",item.ip];
    
        [CYXBaseRequest taskPostWithUrl:API_getProxySpeed(ip) param:nil resultClass:nil success:^(id res, NSURLSessionDataTask *task) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                
            }
            if (!self.firstProxyAddr || !self.firstProxyAddr.length) {
                self.firstProxyAddr = task.currentRequest.URL.absoluteString;
                [self configFirstProxyAddr];
            }
            LRLog(@"%@",task.currentRequest.URL.absoluteString);
        } failure:^(NSError *err) {
            ;
        }];
    }
}

- (void)configFirstProxyAddr{
    NSString *addr = [self.firstProxyAddr stringByReplacingOccurrencesOfString:@"/data/ping" withString:@""];
    if (addr && addr.length) {
        kAppDelegate.bestProxy = addr;
    }
}
@end

@implementation ProxyModel

@end
